<?php
include 'EditDistanceCalculator.php';
$string1 = $string2 = '';
$withTransposionStep = false;
if (isset($_GET['first']) && isset($_GET['second'])) {
	$string1 = $_GET['first'];
	$string2 = $_GET['second'];
}

if (isset($_GET['withTranspositionStep']) &&$_GET['withTranspositionStep'] == 1) {
	$withTransposionStep = true;
}


$edCalc = new EditDistanceCalculator();
$result = $edCalc->calculate($string1, $string2, $withTransposionStep);
$distances = $edCalc->getDistances();
$path = $edCalc->getPath();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style type="text/css">
	*, body, html {
		font-family: Arial;
	}
	table.matrix {
		font-size: 14px;
		border-spacing: 0px !important;
	}
	table.matrix th,
	table.matrix td {
		padding: 5px;
		border: 1px solid #e1e1e1;
		text-align: center;
	}
	
	table.matrix td.marked {
		background-color: yellow;
		color: #000000;
		font-weight: bold;
	}
	input[type="text"] {
		width: 200px;
	}
</style>
</head>
<body>
	<h2>Advanced Algorithmics (HW7)</h2>
	<h3>Damerau–Levenshtein distance</h3>
	
	<form action="" method="get">
		<table>
			<tr>
				<td>First word:</td>
				<td><input type="text" name="first" value="<?php if (isset($_GET['first'])) { echo $_GET['first']; };?>" /></td>
			</tr>
			<tr>
				<td>Second word:</td>
				<td><input type="text" name="second" value="<?php if (isset($_GET['second'])) { echo $_GET['second']; };?>" /></td>
			</tr>
			<tr>
				<td>Number of operations:</td>
				<td><span style="font-size: 20px;font-weight: bold;"><?php echo $result; ?></span></td>
			</tr>
			<tr>
				<td>With transposition step:</td>
				<td><input type="checkbox" name="withTranspositionStep" value="1" <?php if ($withTransposionStep):?>checked="checked"<?php endif;?>/></td>
			</tr>
			<tr>
				<td><input type="submit" value="Send" /></td>
				<td></td>
			</tr>
		</table>
	</form>
	
	<?php if ($string1 != '' || $string2 != ''): ?>
		<table class="matrix">
			<tr>
				<th></th>
				<th></th>
				<?php foreach (str_split($string2) as $i => $letter): ?>
					<th><?php echo $letter; ?></th>
				<?php endforeach; ?>
			</tr>
			<?php foreach ($distances as $i => $row): ?>
				<tr>
					<?php if ($i > 0): ?>
					<th><?php echo $string1[$i-1]?></th>
					
					<?php else: ?>
						<th></th>
					<?php endif; ?>
					<?php foreach ($row as $j => $col):?>
						<td <?php if ($path[$i][$j] == 1): ?>class="marked"<?php endif; ?>><?php echo $col; ?></td>
					<?php endforeach; ?>
				</tr>
			<?php endforeach; ?>
		</table>
	<?php endif; ?>

</body>
</html>
